using System.Text.Json;

namespace RAG.Application.Commands;

/// <summary>
/// API状态码枚举
/// </summary>
public enum ApiStatusCode
{
    /// <summary>
    /// 成功
    /// </summary>
    SUCCESS = 200,
    
    /// <summary>
    /// 错误请求
    /// </summary>
    BAD_REQUEST = 400,
    
    /// <summary>
    /// 未授权
    /// </summary>
    UNAUTHORIZED = 401,
    
    /// <summary>
    /// 禁止访问
    /// </summary>
    FORBIDDEN = 403,
    
    /// <summary>
    /// 未找到
    /// </summary>
    NOT_FOUND = 404,
    
    /// <summary>
    /// 冲突
    /// </summary>
    CONFLICT = 409,
    
    /// <summary>
    /// 验证错误
    /// </summary>
    VALIDATION_ERROR = 422,
    
    /// <summary>
    /// 请求过多
    /// </summary>
    TOO_MANY_REQUESTS = 429,
    
    /// <summary>
    /// 服务器内部错误
    /// </summary>
    INTERNAL_ERROR = 500
}

/// <summary>
/// API响应结果类
/// </summary>
public class ApiResult
{
    /// <summary>
    /// HTTP状态码
    /// </summary>
    public int Code { get; set; }

    /// <summary>
    /// 操作结果消息
    /// </summary>
    public string? Message { get; set; }

    /// <summary>
    /// 业务数据（可为null）
    /// </summary>
    public object? Data { get; set; }

    /// <summary>
    /// 请求路径（仅错误时返回）
    /// </summary>
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;

    public ApiResult()
    {
        // Timestamp 已有默认值，无需再赋值
    }

    /// <summary>
    /// 创建成功响应
    /// </summary>
    /// <param name="message">成功消息</param>
    /// <param name="data">业务数据</param>
    /// <returns>ApiResult实例</returns>
    public static ApiResult Success(string message = "操作成功", object? data = null)
    {
        return new ApiResult
        {
            Code = (int)ApiStatusCode.SUCCESS,
            Message = message,
            Data = data
        };
    }

    public static ApiResult Success(int code, string message, object? data = null)
        => new ApiResult { Code = code, Message = message, Data = data, Timestamp = DateTime.UtcNow };

    /// <summary>
    /// 创建错误响应
    /// </summary>
    /// <param name="code">HTTP状态码</param>
    /// <param name="message">错误消息</param>
    /// <param name="path">请求路径</param>
    /// <param name="details">错误详情</param>
    /// <returns>ApiResult实例</returns>
    public static ApiResult Failed(int code, string message) => new() { Code = code, Message = message };
}

/// <summary>
/// 通用API结果类
/// </summary>
/// <typeparam name="T">返回数据类型</typeparam>
public class ApiResult<T>
{
    public int Code { get; set; } // 使用枚举状态码
    public string Message { get; set; } = "";
    public T? Data { get; set; }
    public bool Succeeded => Code == (int)ApiStatusCode.SUCCESS;

    public DateTime Timestamp { get; set; } = DateTime.UtcNow;

    public static ApiResult<T> Success(T? data = default, string? message = null)
        => new()
        { Code = (int)ApiStatusCode.SUCCESS, Message = message ?? "操作成功", Data = data };

    public static ApiResult<T> Failed(string message, int code = (int)ApiStatusCode.BAD_REQUEST)
        => new()
        { Code = code, Message = message };

    public static ApiResult<T> Failed(int code, string message)
        => new()
        { Code = code, Message = message };

    public static ApiResult<T> Success(int code, string message, T data)
        => new ApiResult<T> { Code = code, Message = message, Data = data, Timestamp = DateTime.UtcNow };
} 